package com.chutoutian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chutoutian.mapper.UserMapper;
import com.chutoutian.po.User;
import com.chutoutian.service.UserService;
import com.chutoutian.untils.SnowflakeIdWorker;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public void add(User user) {
        user.setId(SnowflakeIdWorker.getId());
        user.setCreateTime(new Date());
        userMapper.insert(user);
    }

    @Override
    public User get(String id) {
        return userMapper.selectById(id);
    }

    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    @Override
    public void delete(String id) {
        userMapper.deleteById(id);
    }

    @Override
    public User login(String nickname) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nickname", nickname);
        return userMapper.selectOne(queryWrapper);
    }

}
